package models;


/**
 * @author Micha�
 *
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.Random;

public class Deck {
	
	Stack<Card> cardStack;	//stos
	List<Card> cardSet; 	//ulozona talia
	Random generator;		//gen. liczb pseudolosowych
	
	Deck() throws PokerException
	{
		cardStack = new Stack<Card>();
		
		generator = new Random();
		
		cardSet = new ArrayList<Card>(52);
		
		//TWORZENIE TALII
		
		//kiery
		try
		{
			 cardSet.add(new Card(Card.S_HEARTS, 2));
			 cardSet.add(new Card(Card.S_HEARTS, 3));
			 cardSet.add(new Card(Card.S_HEARTS, 4));
			 cardSet.add(new Card(Card.S_HEARTS, 5));
			 cardSet.add(new Card(Card.S_HEARTS, 6));
			 cardSet.add(new Card(Card.S_HEARTS, 7));
			 cardSet.add(new Card(Card.S_HEARTS, 8));
			 cardSet.add(new Card(Card.S_HEARTS, 9));
			 cardSet.add(new Card(Card.S_HEARTS, 10));
			 cardSet.add(new Card(Card.S_HEARTS, 11));
			 cardSet.add(new Card(Card.S_HEARTS, 12));
			 cardSet.add(new Card(Card.S_HEARTS, 13));
			 cardSet.add(new Card(Card.S_HEARTS, 14));
			
			//kara
			 cardSet.add(new Card(Card.S_DIAMONDS, 2));
			 cardSet.add(new Card(Card.S_DIAMONDS, 3));
			 cardSet.add(new Card(Card.S_DIAMONDS, 4));
			 cardSet.add(new Card(Card.S_DIAMONDS, 5));
			 cardSet.add(new Card(Card.S_DIAMONDS, 6));
			 cardSet.add(new Card(Card.S_DIAMONDS, 7));
			 cardSet.add(new Card(Card.S_DIAMONDS, 8));
			 cardSet.add(new Card(Card.S_DIAMONDS, 9));
			 cardSet.add(new Card(Card.S_DIAMONDS, 10));
			 cardSet.add(new Card(Card.S_DIAMONDS, 11));
			 cardSet.add(new Card(Card.S_DIAMONDS, 12));
			 cardSet.add(new Card(Card.S_DIAMONDS, 13));
			 cardSet.add(new Card(Card.S_DIAMONDS, 14));
			
			//trefle
			 cardSet.add(new Card(Card.S_CLUBS, 2));
			 cardSet.add(new Card(Card.S_CLUBS, 3));
			 cardSet.add(new Card(Card.S_CLUBS, 4));
			 cardSet.add(new Card(Card.S_CLUBS, 5));
			 cardSet.add(new Card(Card.S_CLUBS, 6));
			 cardSet.add(new Card(Card.S_CLUBS, 7));
			 cardSet.add(new Card(Card.S_CLUBS, 8));
			 cardSet.add(new Card(Card.S_CLUBS, 9));
			 cardSet.add(new Card(Card.S_CLUBS, 10));
			 cardSet.add(new Card(Card.S_CLUBS, 11));
			 cardSet.add(new Card(Card.S_CLUBS, 12));
			 cardSet.add(new Card(Card.S_CLUBS, 13));
			 cardSet.add(new Card(Card.S_CLUBS, 14));
			
			//piki
			 cardSet.add(new Card(Card.S_SPADES, 2));
			 cardSet.add(new Card(Card.S_SPADES, 3));
			 cardSet.add(new Card(Card.S_SPADES, 4));
			 cardSet.add(new Card(Card.S_SPADES, 5));
			 cardSet.add(new Card(Card.S_SPADES, 6));
			 cardSet.add(new Card(Card.S_SPADES, 7));
			 cardSet.add(new Card(Card.S_SPADES, 8));
			 cardSet.add(new Card(Card.S_SPADES, 9));
			 cardSet.add(new Card(Card.S_SPADES, 10));
			 cardSet.add(new Card(Card.S_SPADES, 11));
			 cardSet.add(new Card(Card.S_SPADES, 12));
			 cardSet.add(new Card(Card.S_SPADES, 13));
			 cardSet.add(new Card(Card.S_SPADES, 14));
			 
		} catch (PokerException pexc)
		{
			throw new PokerException("Nie uda�o si� utworzy� talii: "+pexc.getMessage());
		}
		
	}
	
	
	//tasuje całą talię
	void shuffleNew() throws PokerException
	{
		cardStack.clear();
		if(cardSet == null || cardSet.size()<52) throw new PokerException("Nie znaleziono talii");
		
		List<Card> tempList = new ArrayList<Card>(cardSet); //tworzy tymczasowa kopie talii
		
		int i;
		int j;
		for(i = 51; i>=0; i--)
		{
			j = generator.nextInt(i+1);			
			cardStack.push(tempList.get(j));
			tempList.remove(j);
		}
		
	}
	
	void shuffleCurrent() 
	{
		List<Card> tempList = new ArrayList<Card>(cardStack);
		cardStack.clear();
		
		int i;
		int j;
		int l = tempList.size();
		for(i = l-1; i>=0; i--)
		{
			j = generator.nextInt(i+1);			
			cardStack.push(tempList.get(j));
			tempList.remove(j);
		}
		
	}
	
	Card pop() throws PokerException
	{
		if(cardStack.size() <1) throw new PokerException("Stos jest pusty");
		return cardStack.pop();
	}
	
	void push(Card c)
	{
		cardStack.push(c);
	}
	
	/*void clearNshow()
	{
		while(!cardStack.empty())
		{

			System.out.print(cardStack.peek().getSuit());
			System.out.print("#");
			System.out.print(cardStack.peek().getFigure());
			System.out.print(" ");
			
			cardStack.pop();
		}
		System.out.println();
	}*/
	

}
